package com.hqyj.erpserver.util;

import java.util.List;

public class RestResult<T> {
    //用于标识后台方法是否成功执行
    private int code; //0-成功； 1-失败
    //提示信息
    private String msg;
    //后台返回单个对象
    private T obj;
    //后台返回的列表数据
    private List<T> list;
    //总页数/记录数
    private long total;

    public RestResult(int code) {
        this.code = code;
    }

    public RestResult(int code, T obj) {
        this.code = code;
        this.obj = obj;
    }

    public RestResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public RestResult(int code,  T obj, String msg) {
        this.code = code;
        this.msg = msg;
        this.obj = obj;
    }

    public RestResult(int code, List<T> list) {
        this.code = code;
        this.list = list;
    }

    public RestResult(int code,  List<T> list, String msg) {
        this.code = code;
        this.msg = msg;
        this.list = list;
    }

    public RestResult(int code, List<T> list, long pages) {
        this.code = code;
        this.list = list;
        this.total = pages;
    }

    //只返回成功状态
    public static <T> RestResult<T> ok(){
        return new RestResult<>(0);
    }

    //返回状态和提示信息
    public static <T> RestResult<T> ok(String msg){
        return new RestResult<>(0, msg);
    }

    //返回成功状态和实体对象
    public static <T> RestResult<T> ok(T obj){
        return new RestResult<>(0, obj);
    }

    //返回成功状态，提示消息，单个实体对象
    public static <T> RestResult<T> ok(T obj, String msg){
        return new RestResult<>(0, obj, msg);
    }

    //返回成功状态和列表数据
    public static <T> RestResult<T> ok(List<T> list){
        return new RestResult<>(0, list);
    }

    //返回成功状态和列表数据
    public static <T> RestResult<T> ok(List<T> list, long total){
        return new RestResult<>(0, list, total);
    }

    //返回成功状态，列表数据， 提示消息
    public static <T> RestResult<T> ok(List<T> list, String msg){
        return new RestResult<>(0, list, msg);
    }

    public static <T> RestResult<T> error(String msg){
        return new RestResult<>(1, msg);
    }

    public static <T> RestResult<T> error(int code, String msg){
        return new RestResult<>(code, msg);
    }



    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getObj() {
        return obj;
    }

    public void setObj(T obj) {
        this.obj = obj;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }
}
